<!--
 * @Author       : xiaowu wwl093000@163.com
 * @Date         : 2023-04-20 16:24:29
 * @LastEditTime : 2023-05-30 14:37:31
 * @LastEditors  : xiaowu wwl093000@163.com
 * @FilePath     : /erp-admin-ui/src/views/distribution/erpAdd/index.vue
 * @Description  : -增值服务
-->
<template>
  <div class="app-container value-add-container" v-loading="loading">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="110px">
      <el-form-item label="有效期时间：">
        <span style="color: #ff6a00;">{{ dataTime || '暂未购买' }}</span>
      </el-form-item>
    </el-form>

    <div class="value-add-content">
      <div
        v-for="(item, index) in serviceErpList"
        :key="index"
        class="content-wrapper"
      >
        <div class="wrapper-title">{{ item.title }}</div>
        <img class="wrapper-img" :src="imgUrl(item.icon)" />
        <div class="wrapper-price">￥{{ item.price }}</div>
        <div class="wrapper-but" @click="buyBtn(item.id)">购买</div>
      </div>
    </div>

    <el-dialog
      title="购买"
      :visible.sync="dialogVisible"
      width="40%"
      center
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :show-close="false"
      destroy-on-close>
      <div class="dialog-content">
        <!-- <div class="dialog-con-item">
          <div class="item-title">订单号：</div>
          <div class="item-text">KID567856932475F600</div>
        </div> -->
        <div class="dialog-con-item">
          <div class="item-title">名称：</div>
          <div class="item-text">{{ serviceInfo.title }}</div>
        </div>
        <div class="dialog-con-item">
          <div class="item-title">详情：</div>
          <div class="item-text">{{ serviceInfo.details }}</div>
        </div>
        <div class="dialog-con-item">
          <div class="item-title">价格：</div>
          <div class="item-text">￥{{ serviceInfo.price }}</div>
        </div>
        <div class="dialog-con-item">
          <div class="item-title">有效期：</div>
          <div class="item-text">
            {{ serviceInfo.expirationTime }}
            {{ serviceInfo.expirationType == '1' ? '月' : serviceInfo.expirationType == '2' ? '季' : serviceInfo.expirationType == '3' ? '年' : ''}}
          </div>
        </div>
        <div class="dialog-con-item">
          <div class="item-title">支付方式：</div>
          <div class="item-text item-checkbox">
            <el-radio v-model="paymentParmas.payWay" label="1">支付宝</el-radio>
            <!-- <el-radio v-model="paymentParmas.payWay" label="2">微信</el-radio> -->
          </div>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false" :disabled="payLoading">取 消</el-button>
        <el-button type="primary" @click="createErpAmountBtn" :loading="payLoading" :disabled="payLoading">购买服务</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { serviceErpList, serviceDetail, userExpirationTime, createErpAmount } from "@/api/distribution/index";
import store from '@/store';

export default {
  name: "ErpAdd",
  data() {
    return {
      // 遮罩层
      loading: false,
      queryParams: {
      },
      dialogVisible: false,
      serviceErpList: [],
      serviceInfo: {},
      paymentParmas: {
        payWay: '1',
        userId: store.getters.userId,
        serviceId: undefined
      },
      payLoading: false,
      dataTime: null
    };
  },
  created() {
    this.getUserExpirationTime();
    this.getList();
  },
  computed: {
    imgUrl() {
      return (val) => {
        if (val.indexOf('http://') === -1 && val.indexOf('https://') === -1) {
          return store.getters.domain + val;
        } else {
          return val;
        }
      }
    }
  },
  methods: {
    // 获取列表
    getList() {
      this.loading = true;
      serviceErpList({userId: store.getters.userId}).then(res => {
        this.serviceErpList = res.rows;
        this.loading = false;
      });
    },
    // 获取有效期时间
    getUserExpirationTime() {
      userExpirationTime(store.getters.userId).then(res => {
        this.dataTime = res.data
      })
    },
    // 购买服务
    createErpAmountBtn() {
      this.payLoading = true;
      createErpAmount(this.paymentParmas).then(res => {
        this.payLoading = false;
        this.dialogVisible = false;
        this.$router.push(`/distribution1/recordPage`);
      })
    },
    // 打开弹框
    buyBtn(id) {
      this.dialogVisible = true;
      this.paymentParmas.serviceId = id;
      serviceDetail(id).then(res => {
        this.serviceInfo = res.data;
      })
    }
  }
};
</script>

<style lang="scss">
.value-add-container {
  .value-add-content {
    margin-top: 20px;
    display: flex;
    flex-wrap: wrap;
  
    .content-wrapper {
      width: calc((100% - 160px) / 4);
      margin: 0 20px 20px;
      padding: 50px 40px 40px;
      border-radius: 10px;
      display: flex;
      flex-direction: column;
      align-items: center;
  
      .wrapper-title {
        font-size: 24px;
        font-weight: 700;
        color: #ffffff;
        line-height: 30px;
      }
  
      .wrapper-img {
        width: 100%;
        display: block;
        height: 200px;
        margin-top: 30px;
        object-fit: cover;
      }
  
      .wrapper-price {
        font-size: 18px;
        line-height: 20px;
        color: #ffffff;
        font-weight: 700;
        margin: 20px 0 10px;
      }
  
      .wrapper-but {
        width: 180px;
        height: 50px;
        border-radius: 10px;
        line-height: 50px;
        font-size: 18px;
        font-weight: 400;
        color: #ffffff;
        text-align: center;
        cursor: pointer;
        &:hover {
          background: #ffffff !important;
          color: #999999;
        }
      }
      .wrapper-but-success {
        background: #ffffff !important;
        color: #999999;
      }
  
      &:nth-child(2n-1) {
        background: #4f98f2;
        box-shadow: -5px 0px 10px 5px rgba(129,185,239,0.30); 
        .wrapper-but {
          background: #1C68C6;
        }
      }
  
      &:nth-child(2n) {
        background: #F4BE48;
        box-shadow: -5px 0px 10px 5px rgba(249,193,69,0.30);
        .wrapper-but {
          background: #C0880E;
        }
      }
      &:nth-child(3n) {
        background: #87A0E7;
        box-shadow: -5px 0px 10px 5px rgba(119,113,255,0.30);
        .wrapper-but {
          background: #4F72D5;
        }
      }
    }
  
  }
  .el-dialog {
    border-radius: 30px;
  }
  .dialog-content {
    border-top: 1px solid #CCCCCC;
    border-bottom: 1px solid #CCCCCC;
    padding: 20px;
    .dialog-con-item {
      border-bottom: 1px dashed #CCCCCC;
      padding: 30px 0;
      display: flex;
      align-items: flex-start;
      font-size: 16px;
      line-height: 30px;
      font-weight: 400;
      .item-title {
        text-align: left;
        color: #666666;
        width: 80px;
      }
      .item-text {
        flex: 1;
        color: #000000;
        padding: 0 12px;
      }
      &:last-child {
        border: none;
      }
    }
  }
}
</style>